#!/bin/bash
function srv_optimize() {
echo -e "${blue}当前正在执行的操作为:${nocolor} ${green}禁用系统服务${nocolor}"
disable_srv=
disable_srv_=
disable_srv_max=
echo -ne "
请选择要停止并禁用的服务
${green}1.${nocolor}防火墙服务
${green}2.${nocolor}邮件服务
"

while [[ ! $disable_srv_ =~ ^[1-2]+$ || $disable_srv_max -gt 2 ]]
do
   echo -ne "请输入数字，多个选项间以空格分开，或者输入N，返回: "
   read disable_srv
   disable_srv_=$(echo $disable_srv|sed 's/ /\n/g'|sed '/^$/d'|sort -nu|tr -d '\n')
   disable_srv_max=$(echo $disable_srv|sed 's/ /\n/g'|sed '/^$/d'|sort -rnu|head -n1)
   if [[ $disable_srv = [Nn] ]];then
      return 1
   fi
done

echo ""
echo -e "${cyan}开始禁用系统服务${nocolor}"
sleep 0.15

disable_srv=$(echo $disable_srv|sed 's/ /\n/g'|sed '/^$/d'|sort -u)
if [[ $system_ver = [456] ]];then
    disable_srv=${disable_srv//1/iptables}
    disable_srv=${disable_srv//2/sendmail}
    for srv in $disable_srv
    do
      echo -e "${Info} 正在禁用服务 $srv"
      service $srv stop
      chkconfig --level 2345 $srv off
    done
else
    disable_srv=${disable_srv//1/firewalld}
    disable_srv=${disable_srv//2/postfix}
    for srv in $disable_srv
    do
      echo -e "${Info} 正在禁用服务 $srv"
      systemctl stop $srv
      systemctl disable $srv
    done
fi

echo ""
disable_srv__=$(echo "$disable_srv"|sed ':ba;N;s/\n/, /g;b ba')
echo -ne "${cyan}已经禁用系统服务${disable_srv__}. 请按任意键返回!${nocolor}"
read -s -n1 goback
echo -e ""

}